<?php

class ImportPaymentController extends Controller
{

    public function filters()
    {
        return array(
            'accessControl', // perform access control for CRUD operations
            array(
                'application.filters.RequireLoginFilter',
            ),
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules()
    {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions
                'users' => array('@'),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

    public function actionIndex()
    {
        $model = new PaymentUploadForm();
        $processed = false;
        if (isset($_POST['PaymentUploadForm']))
        {
            $model->attributes = $_POST['PaymentUploadForm'];
            if ($model->validate())
            {
                if ($model->process())
                {
                    $processed = $model->processed;
                }
            }
            else
            {
                var_dump($model->getErrors());
            }
        }

        $this->render('index', array(
            'model' => $model,
            'processed' => $processed
        ));
    }
   public function actionQb_paymentsapplied() {
        $model = new PaymentAppliedUploadForm();     $processed = false;
        if (isset($_POST['PaymentAppliedUploadForm'])) {
            $model->attributes = $_POST['PaymentAppliedUploadForm'];
            if ($model->validate()) {
               $delimiter = ',';
                $file = CUploadedFile::getInstance($model, 'file');
                if ($file != null) {
                    $file->saveAs('payment/' . $file->name);
                }
                $dataFile = fopen('payment/' . $file->name, 'r');
                if ($dataFile !== false) {
     $processed = true;
                    QbPaymentsapplied::model()->deleteAll();
                    $first = true;
                    while ($line = fgets($dataFile)) {
                       $dataModel = new QbPaymentsapplied();     
                        if ($first) {
                            $first = false;
                            continue;
                        }

                        $lineArray = safeExplode($delimiter, $line);
                        $i = 0;
                        $attrList = $dataModel->attributes;
                       
                       
                        foreach ($attrList as $key=>$value){
                           $attrList[$key] = $lineArray[$i];
                            $i++;
                        }
                      
                        $dataModel->attributes = $attrList;
                      $dataModel->save();
                 
                    }
                }else {

                    echo 'File not avaible';
                }
            }
        } 


        $this->render('qb_paymentsapplied', array(
            'model' => $model,
            'processed' => $processed
        ));
    }

     public function actionQbjournals() {
        $model = new PaymentAppliedUploadForm();     $processed = false;
        if (isset($_POST['PaymentAppliedUploadForm'])) {
            $model->attributes = $_POST['PaymentAppliedUploadForm'];
            if ($model->validate()) {
               $delimiter = ',';
                $file = CUploadedFile::getInstance($model, 'file');
                if ($file != null) {
                    $file->saveAs('payment/' . $file->name);
                }
                $dataFile = fopen('payment/' . $file->name, 'r');
                if ($dataFile !== false) {
     $processed = true;
                    QuickbooksJournals::model()->deleteAll();
                    $first = true;
                    while ($line = fgets($dataFile)) {
                       $dataModel = new QuickbooksJournals();     
                        if ($first) {
                            $first = false;
                            continue;
                        }

                        $lineArray = safeExplode($delimiter, $line);
                        $i = 0;
                        $attrList = $dataModel->attributes;
                       
                       
                        foreach ($attrList as $key=>$value){
                           $attrList[$key] = trim ($lineArray[$i]);
                            $i++;
                        }
                      
                        $dataModel->attributes = $attrList;
                       
                        $dataModel->save();
                 
                    }
                }else {

                    echo 'File not avaible';
                }
            }
        } 


        $this->render('qb_paymentsapplied', array(
            'model' => $model,
            'processed' => $processed
        ));
    }

}
